package com.example.administrator.myapplication.View.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Http.Api;
import com.example.administrator.myapplication.Presenter.LoginPresenter;
import com.example.administrator.myapplication.Presenter.LogonPresenter;
import com.example.administrator.myapplication.R;

public class LogonActivity extends AppCompatActivity implements View.OnClickListener,ILogonView{
    private EditText et_loginname;
    private EditText et_username;
    private EditText et_phone;
    private EditText et_email;
    private EditText et_pwd;
    private EditText et_pwd_again;
    private TextView tv_logon;
    private LogonPresenter logonPresenter;
    private boolean phoneUnique=false;
    private boolean emailUnique=false;
    private boolean loginNameUnique=false;
    private int unique_ret_count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logon);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(Color.parseColor("#6A9C9D"));
        }
        initView();
    }

    public void  initView(){
        et_username = findViewById(R.id.et_username);
        et_pwd = findViewById(R.id.et_pwd);
        et_loginname = findViewById(R.id.et_loginname);
        tv_logon = findViewById(R.id.tv_logon);
        et_phone = findViewById(R.id.et_phone);
        et_email = findViewById(R.id.et_email);
        et_pwd_again = findViewById(R.id.et_pwd_again);
        logonPresenter = new LogonPresenter(this);
        tv_logon.setOnClickListener(this);
    }

    private int checkInput(){
        int count = 0;
        if (TextUtils.isEmpty(et_loginname.getText().toString())){
            et_loginname.setError("用户名不能为空");
            count++;
        }
        if (TextUtils.isEmpty(et_username.getText().toString())){
            et_username.setError("用户昵称不能为空");
            count++;
        }
        if (TextUtils.isEmpty(et_phone.getText().toString())){
            et_phone.setError("手机号码不能为空");
            count++;
        }
        if (TextUtils.isEmpty(et_email.getText().toString())){
            et_email.setError("电子邮箱不能为空");
            count++;
        }
        if(TextUtils.isEmpty(et_pwd.getText().toString())){
            et_pwd.setError("密码不能为空");
            count++;
        }
        if(TextUtils.isEmpty(et_pwd_again.getText().toString())){
            et_pwd_again.setError("密码不能为空");
            count++;
        }
        if(!TextUtils.equals(et_pwd.getText().toString(),et_pwd_again.getText().toString())){
            et_pwd_again.setError("两次密码不一致");
            count++;
        }
        return count;
    }
    private  void checkUnique(){
        String loginname = et_loginname.getText().toString();
        String phone = et_phone.getText().toString();
        String email = et_email.getText().toString();
        logonPresenter.checkPhoneUnique(phone);
        logonPresenter.checkLoginNameUnique(loginname);
        logonPresenter.checkEmailUnique(email);
    }

    private void logon(){

        /**
         * 输入框检查
         * */
        if(checkInput()!=0){
            return;
        }

        /**
         * 唯一性检查
         * */
        unique_ret_count=0;
        checkUnique();
        /**
         * 判断异步请求是否完成
         * */
        Thread myThread=new Thread(){//创建子线程
            @Override
            public void run() {
                int timeout=0;
                while(unique_ret_count!=3){
                        try {
                            sleep(50);
                            timeout+=50;
                            if(timeout>=10000)
                                return;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                }
                if(loginNameUnique&&phoneUnique&&emailUnique)
                    submitLogon();
            }
        };
        myThread.start();
        }

    public void submitLogon(){
        User user = new User();
        user.setLoginName(et_loginname.getText().toString());
        user.setUserName(et_username.getText().toString());
        user.setPhonenumber(et_phone.getText().toString());
        user.setEmail(et_email.getText().toString());
        user.setPassword(et_pwd.getText().toString());
        user.setSex("0");
        logonPresenter.logon(user);
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showSuccess(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
        Intent it=new Intent(getApplicationContext(),LoginActivity.class);
        startActivity(it);
        finish();
    }

    @Override
    public void showLoginNameNotUnique() {
        et_loginname.setError("用户名已存在");
        unique_ret_count++;
        loginNameUnique=false;
    }

    @Override
    public void showPhoneNotUnique() {
        et_phone.setError("手机号码已存在");
        unique_ret_count++;
        phoneUnique=false;
    }

    @Override
    public void showEmailNotUnique() {
        et_email.setError("Email已存在");
        unique_ret_count++;
        emailUnique=false;
    }

    @Override
    public void showLoginNameIsUnique() {
        unique_ret_count++;
        loginNameUnique=true;
    }

    @Override
    public void showPhoneIsUnique() {
        unique_ret_count++;
        phoneUnique=true;
    }

    @Override
    public void showEmailIsUnique() {
        unique_ret_count++;
        emailUnique=true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_logon:
                logon();
                break;
        }
    }
}
